package com.example.lenovo.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class GameView extends View {
    public int mPosX;
    public int mPosY;

    public Paint mPaint;

    public boolean mRun = true;

    public int mStepX = 3;
    public int mStepY = 3;

    public int mWindowWidth;
    public int mWindowHeight;

    public int mBoardPosX;
    public int mBoardPosY;

    int lastX = 0, lastY = 0;

    public OnNeedInvalidateCaller mCaller = null;

    public void setOnNeedInvalideCaller(OnNeedInvalidateCaller caller) {
        mCaller = caller;
    }

    public GameView(final Context context) {
        super(context);

        mPosX = 150;
        mPosY = 150;
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        mWindowHeight = dm.heightPixels;
        mWindowWidth = dm.widthPixels;

        mBoardPosX = (mWindowWidth - 100) >> 1;
        mBoardPosY = mWindowHeight - 120;


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mRun) {
                    if (mPosX > mWindowWidth) {
                        mStepX = -3;
                    } else if (mPosX < 0) {
                        mStepX = 3;
                    }

                    if (mPosY > mWindowHeight) {
                        mRun = false;
                        Log.i("GameView", "Game Failed");
                    }

                    mPosX += mStepX;
                    mPosY += mStepY;

                    if (mPosY > mBoardPosY - 10 && mPosY < mBoardPosY + 10) {
                        if (mPosX < mBoardPosX + 100 && mPosX > mBoardPosX) {
                            mStepY = -mStepY;
                        }
                    }
                    if (mCaller != null) {
                        mCaller.OnNeedInvalidate();
                    }
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(Color.BLACK);
        canvas.drawCircle(mPosX, mPosY, 50, mPaint);

        mPaint.setColor(Color.GREEN);
        canvas.drawRect(mBoardPosX, mBoardPosY, mBoardPosX + 100, mBoardPosY + 10, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();


                break;
            case MotionEvent.ACTION_MOVE:
                int currentX = (int) event.getRawX();

                if (currentX > lastX) {
                    mBoardPosX += 10;
                } else {
                    mBoardPosX -= 10;
                }

                lastX = currentX;

                break;
            case MotionEvent.ACTION_UP:
                //松开

                break;
        }

        return true;
    }



    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        mRun = false;
    }
    interface OnNeedInvalidateCaller{
        void OnNeedInvalidate();
    }

}

